<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表</title>
    <link rel="stylesheet" href="./css/common.css">
    <link rel="stylesheet" href="./css/list.css">
</head>

<body>
    <!--导航栏-->
    <div class="nav">
        <!--logo-->
        <img src="./images/book.png" alt="">
        <div class="content">
            <span>我的博客系统</span>
            <div class="opts">
                <a href="./blog_list.html">主页</a>
                <a href="./blog_eidt.html">写博客</a>
                <a href="logout">注销</a>
            </div>
        </div>
    </div>
    <!--正文部分-->
    <div class="container">
        <!--左边区域-->
        <div class="container-left">
            <div class="card">
                <img src="./images/003.jpg" alt="">
                <h3> </h3>
                <a href="#">GitHub</a>
                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <divc class="counter">
                    <span>2</span>
                    <span>1</span>
                </divc>
            </div>
        </div>
        <!--右边区域-->
        <div class="container-right">
            <!-- 博客正文
                <div class="blog-content">
                    <div class="blog-title">
                        这是我的第一篇博客
                    </div>
                    <div class="blog-datetime">
                        2023-03-18 10:50:06
                    </div>
                    <div class="content">
                        <p>这是我的第一篇博客内容Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui temporibus a incidunt
                            totam, tempora iste facere iusto aliquid odit voluptatum veniam optio dolorum, harum fugit
                            praesentium quo esse exercitationem delectus?</p>
                        <p>这是我的第一篇博客内容Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui temporibus a incidunt
                            totam, tempora iste facere iusto aliquid odit voluptatum veniam optio dolorum, harum fugit
                            praesentium quo esse exercitationem delectus?</p>
                    </div>
                    </div>
                    <div class="aEl">
                        <a href="./blog_details.html">查看全文 &gt;&gt; </a>
                    </div> -->
        </div>
    </div>
</body>
<script src="./js/jquery-3.6.3.min.js"></script>
<script src="./js/common.js"></script>
<script>
    // 发送AJAX 请求
    $.ajax({
        // 方法类型
        type: 'get',
        // URL
        url: 'blog',
        // 回调
        success: function (data) {
            console.log(data);
            // 构建页面的方法
            buildBlogList(data);
        },
        error: function () {
            console.log('获取博客列表失败');
        }
    });

    // 定义构建页面的方法
    function buildBlogList(data) {
        // <div class="blog-content">
        //     <div class="blog-title">
        //         这是我的第一篇博客
        //     </div>
        //     <div class="blog-datetime">
        //         2023-03-18 10:50:06
        //     </div>
        //     <div class="content">
        //         内容
        //     </div>
        //     <div class="aEl">
        //         <a href="./blog_details.html?blogId=1">查看全文 &gt;&gt; </a>
        //     </div>
        //     <hr>
        // </div>
        // 获取右侧容器DIV
        let containerEl = document.querySelector(".container-right");

        data.forEach(blog => {
            //创建DIV
            let blogEl = document.createElement('div');
            blogEl.className = 'blog-content';
            //创建title的div
            let titleEl = document.createElement('div');
            titleEl.className = 'blog-title';
            titleEl.innerHTML = blog.title;
            blogEl.appendChild(titleEl);

            //创建datetime的div
            let datetimeEl = document.createElement('div');
            datetimeEl.className = 'blog-datetime';
            datetimeEl.innerHTML = formatDate(blog.createTime);
            blogEl.appendChild(datetimeEl);

            //创建content的div
            let contentEl = document.createElement('div');
            contentEl.className = 'content';
            contentEl.innerHTML = blog.content;
            blogEl.appendChild(contentEl);

            //创建 a 标签的div
            let aDivEl = document.createElement('div');
            aDivEl.className = 'aEl';
            let aEl = document.createElement('a');
            aEl.href = 'blog_details.html?blogId=' + blog.blogId;
            aEl.innerHTML = '查看全文 &gt;&gt; ';
            aDivEl.appendChild(aEl);
            blogEl.appendChild(aDivEl);

            //创建hr
            let hrEl = document.createElement('hr');
            blogEl.appendChild(hrEl);

            //获取父容器
            containerEl.appendChild(blogEl);
        });
    }

    $.ajax({
        type: 'get',
        url: 'user',
        success: function (data) {
            console.log("检验通过...");
            //获取用户名标签 替换成具体用户名的值
            let usernameEl = document.querySelector('.container-left>.card>h3');
            usernameEl.innerHTML = data.username;
        },
        statusCode: {
            403: function () {
                location.assign('blog_login.html');
            }
        }
    });
</script>

</html>